home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / ge_cool.lha / GE_COOL2.1 / src / Association / Association.h < prev    next >
C/C++ Source or Header  |  1992-05-27  |  13KB  |  316 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: MJF 05/22/89 -- Initial design.
  13. // Updated: JCB 06/26/89 -- Implementation.
  14. // Updated: MBN 08/19/89 -- Changed template usage to reflect new syntax
  15. // Updated: MBN 08/24/89 -- Added conditional exception handling and base class
  16. // Updated: LGO 10/02/89 -- Removed destructor - Vector does the work
  17. // Updated: MBN 10/07/89 -- Removed put(Pair<Ktype,Vtype>) method and added
  18. //                          missing set_key_compare/set_value_compare methods
  19. // Updated: MBN 10/11/89 -- Changed "current_position" to "curpos" 
  20. // Updated: LGO 10/16/89 -- Re-wrote operator== to be const
  21. // Updated: MBN 10/19/89 -- Added optional argument to set_compare methods
  22. // Updated: MBN 11/01/89 -- Added constructor with user-provided storage param
  23. // Updated: LGO 11/09/89 -- Inherit publicly from Vector to workaround bugs
  24. //                          in Glockenspiel C++ 1.2 for OS/2
  25. // Updated: MBN 01/31/89 -- Added current_position() member function
  26. // Updated: MBN 02/22/90 -- Changed size arguments from long to unsigned long
  27. // Updated: MJF 03/12/90 -- Added group names to RAISE
  28. // Updated: MJF 06/30/90 -- Added base class name to constructor initializer
  29. // Updated: VDN 02/21/92 -- New lite version
  30. // Updated: VDN 04/20/92 -- Search most recent first, ordering not preserved.
  31. //                           
  32. //
  33. // The Association<Ktype,Vtype> class is privatly derived from the Vector<Type>
  34. // class and  is used  to implement a  collection of  pairs  that  is privately
  35. // derived from the Vector class.  The first of the pair is called the  key and
  36. // the  second  of the pair is called  the value.  The Association<Ktype,Vtype>
  37. // class implements a  single dimensional vector  parameterized over two types.
  38. // The first type specifies the type of the key, and  the second type specifies
  39. // the  type  of  the  value.  The Association<Ktype,Vtype>  class inherits the
  40. // dynamic  growth  capability of  the  Vector class.    The growth size can be
  41. // determined by the value  of a  static  variable for the class as  a whole or
  42. // from  a growth ratio for each  instance  of the  class.   In addition, fixed
  43. // length vectors are also supported  by  setting the  value  of the allocation
  44. // size variable to zero.
  45. //
  46. // Since  Association is  a specialization of   the Vector class, there are  no
  47. // extra data slots needed in  the private section.   The protected section has
  48. // two static data  slots for  the class  as a whole  that contain pointers  to
  49. // comparison functions for  the key   and the  value types, repectively.   The
  50. // default  compare function is operator== for  the appropriate type. There are
  51. // four constructors for the Association class.  The first constructor takes no
  52. // arguments  and creates an  empty Association object.  The second constructor
  53. // takes a single argument specifying the initial size of the Association.  The
  54. // third  constructor takes two  arguments,  the first a  pointer to a block of
  55. // user-provided  storage and the  second providing indicating  the  number  of
  56. // elements it  can  hold.  The  fourth constructor  is  similar to  the third,
  57. // except that   it  takes a variable number   of arguments  to  allow  for the
  58. // initialization   of  any  number of key/value  pairs.    Finally,  the fifth
  59. // constructor  takes a single  argument  consisting  of    a  reference to   a
  60. // Association and duplicates its size and element values.
  61. //
  62. // Due to  the  private  inheritance  of  the  Vector  class,  the only methods
  63. // available for the Association class  are those that are  explicitly defined.
  64. // Methods are available to set the compare functions  for the key and value, a
  65. // resize method, and get, get key,  and remove methods  used  to add, inspect,
  66. // and remove pairs.  In addition, the Association<Ktype,Vtype> class maintains
  67. // the notion of a current position and  provides reset, next,  prev, find, and
  68. // value methods for working with the  pair at  the current position.  Finally,
  69. // the assignment, equality and inequality, and output operators are overloaded
  70. // for the Association class.
  71. //
  72. // Since Association is an unordered set, it should behave more like a Shuffle,
  73. // with ordering not preserved when elements are removed or inserted. Search
  74. // is done from the end to the begining, and so is more like most recent first.
  75.  
  76. #ifndef ASSOCIATIONH                // If we have not defined class
  77. #define ASSOCIATIONH                // indicate that it is done now
  78.  
  79. #ifndef VECTORH                    // If have not defined Vector
  80. #include <cool/Vector.h>                // Include header file
  81. #endif
  82.  
  83. #ifndef PAIRH                    // If we have not defined Pair
  84. #include <cool/Pair.h>                // Include header file
  85. #endif
  86.  
  87. template <class Ktype, class Vtype> CoolAssociation {
  88.   typedef Boolean (*CoolAssociation<Ktype,Vtype>_Key_Compare)
  89.     (const Ktype&, const Ktype&);
  90.   typedef Boolean (*CoolAssociation<Ktype,Vtype>_Value_Compare)
  91.     (const Vtype&, const Vtype&);
  92.   DECLARE CoolPair<Ktype,Vtype>;        // Declare Pair object type
  93.   DECLARE CoolVector<CoolPair<Ktype,Vtype>>;    // Declare Vector of Pairs
  94. }
  95.  
  96. #ifndef ASSOC_STATE
  97. #define ASSOC_STATE
  98. typedef long CoolAssociation_state;        // Current position state
  99. #endif
  100.  
  101. template <class Ktype, class Vtype>
  102. class CoolAssociation<Ktype,Vtype> : private CoolVector<CoolPair<Ktype,Vtype>> {
  103. public:
  104.   CoolAssociation<Ktype,Vtype>();        // Simple constructor
  105.   CoolAssociation<Ktype,Vtype>(unsigned long);    // constructor w/ element count
  106.   CoolAssociation<Ktype,Vtype>(void*, unsigned long); // Assoc with static storage
  107.   CoolAssociation<Ktype,Vtype>(const CoolAssociation<Ktype,Vtype>&); // Copy constr
  108.   ~CoolAssociation<Ktype,Vtype>();            // Destructor
  109.  
  110.   inline void resize(long);            // Resize for at least count
  111.   inline long length();                // Return number of elements
  112.   inline long capacity();            // Max. number of elements
  113.   inline long set_length(long);            // Set number of elements
  114.   inline void set_growth_ratio(float);        // Set growth percentage
  115.   inline void set_alloc_size(int);        // Set alloc size
  116.  
  117.   void set_key_compare (CoolAssociation<Ktype,Vtype>_Key_Compare = NULL);
  118.   void set_value_compare (CoolAssociation<Ktype,Vtype>_Value_Compare = NULL);
  119.  
  120.   inline void reset();                // Invalidate current position
  121.   inline void clear();                // Clear objects from assoc
  122.   inline Boolean next();            // Advance current position
  123.   inline Boolean prev();            // Backup current position
  124.   inline Vtype& value();            // Value at current position
  125.   inline const Ktype& key() const;        // Key at current position
  126.   inline CoolAssociation_state& current_position (); // Set/Get current position
  127.  
  128.   Boolean find(const Ktype&);            // Find/set current position
  129.   Boolean get(const Ktype&, Vtype&);        // Get associated value for key
  130.   Boolean get_key(const Vtype&, Ktype&) const;    // Get associated key for value
  131.  
  132.   Boolean put(const Ktype&, const Vtype&);    // Add/update key/value pair
  133.   Boolean remove(const Ktype&);            // Removes pair with key
  134.   Vtype remove();                // Removes pair at cur pos
  135.  
  136.   inline CoolAssociation<Ktype,Vtype>& operator=(const CoolAssociation<Ktype,Vtype>&);
  137.  
  138.   Boolean operator==(const CoolAssociation<Ktype,Vtype>&) const;
  139.   inline Boolean operator!=(const CoolAssociation<Ktype,Vtype>&) const;
  140.  
  141.   friend ostream& operator<< (ostream&,const CoolAssociation<Ktype,Vtype>&);
  142.   inline friend ostream& operator<< (ostream&,const CoolAssociation<Ktype,Vtype>*);
  143.  
  144. protected:
  145.   static CoolAssociation<Ktype,Vtype>_Key_Compare compare_keys_s;
  146.   static CoolAssociation<Ktype,Vtype>_Value_Compare compare_values_s;
  147.  
  148.   friend Boolean CoolAssociation<Ktype,Vtype>_keys_eql(const Ktype&, const Ktype&);
  149.   friend Boolean CoolAssociation<Ktype,Vtype>_values_eql(const Vtype&,const Vtype&);
  150. };
  151.  
  152. // void reset () -- Set current position to INVALID.
  153. // Input:           None
  154. // Output:          None
  155.  
  156. template <class Ktype, class Vtype>
  157. inline void CoolAssociation<Ktype,Vtype>::reset() {
  158.   CoolVector<CoolPair<Ktype,Vtype>>::reset();
  159. }
  160.  
  161.  
  162. // void clear () -- Clear all objects from association
  163. // Input:           None
  164. // Output:          None
  165.  
  166. template <class Ktype, class Vtype>
  167. inline void CoolAssociation<Ktype,Vtype>::clear() {
  168.   CoolVector<CoolPair<Ktype,Vtype>>::clear();
  169. }
  170.  
  171.  
  172. // Boolean next () -- Increment current position. If INVALID, set to first.
  173. // Input:             None
  174. // Output:            TRUE/FALSE
  175.  
  176. template <class Ktype, class Vtype>
  177. inline Boolean CoolAssociation<Ktype,Vtype>::next() {
  178.   return CoolVector<CoolPair<Ktype,Vtype>>::next();
  179. }
  180.  
  181.  
  182. // Boolean prev () -- Decrement current position. If INVALID, set to last.
  183. // Input:             None
  184. // Output:            TRUE/FALSE
  185.  
  186. template <class Ktype, class Vtype>
  187. inline Boolean CoolAssociation<Ktype,Vtype>::prev() {
  188.   return CoolVector<CoolPair<Ktype,Vtype>>::prev();
  189. }
  190.  
  191.  
  192. // void resize () -- Adjust the memory size of object to accomodate some size
  193. // Input:            Interger number of elements to hold
  194. // Output:           None
  195.  
  196. template <class Ktype, class Vtype>
  197. inline void CoolAssociation<Ktype,Vtype>::resize (long n) {
  198.   CoolVector<CoolPair<Ktype,Vtype>>::resize(n);
  199. }
  200.  
  201.  
  202. // long length () -- Return the number of elements in object.
  203. // Input:            None
  204. // Output:           Integer representing number of elements
  205.  
  206. template <class Ktype, class Vtype>
  207. inline long CoolAssociation<Ktype,Vtype>::length() {
  208.   return CoolVector<CoolPair<Ktype,Vtype>>::length();
  209. }
  210.  
  211.  
  212. // long capacity () -- Return maximum number of elements object can hold.
  213. // Input:              None
  214. // Output:             Integer value of maximum number of elements
  215.  
  216. template <class Ktype, class Vtype>
  217. inline long CoolAssociation<Ktype,Vtype>::capacity () {
  218.   return this->CoolVector::capacity();
  219. }
  220.  
  221.  
  222. // long set_length () -- Set the number of elements in a object.
  223. // Input:                Length value
  224. // Output:               Integer representing number of elements
  225.  
  226. template <class Ktype, class Vtype>
  227. inline long CoolAssociation<Ktype,Vtype>::set_length(long n) {
  228.   return CoolVector<CoolPair<Ktype,Vtype>>::set_length(n);
  229. }
  230.  
  231.  
  232. // void set_growth_ratio () -- Set the growth percentage for the object
  233. // Input:                      Float ratio
  234. // Output:                     None
  235.  
  236. template <class Ktype, class Vtype>
  237. inline void CoolAssociation<Ktype,Vtype>::set_growth_ratio(float n) {
  238.   CoolVector<CoolPair<Ktype,Vtype>>::set_growth_ratio(n);
  239. }
  240.  
  241.  
  242. // void set_alloc_size () -- Set the default allocation size growth rate.
  243. // Input:                    Growth size in number of elements
  244. // Output:                   None
  245.  
  246. template <class Ktype, class Vtype>
  247. inline void CoolAssociation<Ktype,Vtype>::set_alloc_size(int n){
  248.   CoolVector<CoolPair<Ktype,Vtype>>::set_alloc_size(n);
  249. }
  250.  
  251.  
  252. // Ktype& key () -- Get key (first) item in pair object at current position
  253. // Input:           None
  254. // Output:          Reference to Key value of pair
  255.  
  256. template <class Ktype, class Vtype>
  257. inline const Ktype& CoolAssociation<Ktype,Vtype>::key() const {
  258.   return (this->data[this->curpos].get_first());
  259. }
  260.  
  261.  
  262. // Vtype& value () -- Return value at current position.
  263. // Input:             None
  264. // Output:            Type reference to value at current position
  265.  
  266. template <class Ktype, class Vtype>
  267. inline Vtype& CoolAssociation<Ktype,Vtype>::value() {
  268.   return (this->data[this->curpos].second());
  269. }
  270.  
  271.  
  272. // Boolean operator!= -- Test for inequality of the data of two objects
  273. // Input:                Reference to objects
  274. // Output:               TRUE/FALSE
  275.  
  276. template <class Ktype, class Vtype>
  277. inline Boolean CoolAssociation<Ktype,Vtype>::operator!=(const CoolAssociation<Ktype, Vtype>& a) const {
  278.   return (!this->operator==(a));
  279. }
  280.  
  281.  
  282.  
  283. // operator= -- Overload the assignment operator to copy the elements
  284. //              in one CoolAssociation to another. If there are more elements in
  285. //              the source than the destination and the destination is
  286. //              not of static size, then storage will be allocated and
  287. //              the destination CoolAssociation will grow.
  288. // Input:       Reference to CoolAssociation
  289. // Output:      Reference to copied CoolAssociation object
  290.  
  291. template <class Ktype, class Vtype>
  292. inline CoolAssociation<Ktype,Vtype>& CoolAssociation<Ktype,Vtype>::operator=(const CoolAssociation<Ktype,Vtype>& a) {
  293.   CoolVector<CoolPair<Ktype,Vtype>>::operator=(a);
  294.   return *this;
  295. }
  296.  
  297.  
  298. // operator<< -- Overload the output operator for pointer to CoolAssociation
  299. // Input:        ostream reference, Assocition pointer
  300. // Output:       CoolAssociation data is output to ostream
  301.  
  302. template <class Ktype, class Vtype> CoolAssociation {
  303. inline ostream& operator<< (ostream& os, const CoolAssociation<Ktype,Vtype>* v) {
  304.   return operator<< (os, *v);
  305. }}
  306.  
  307.   
  308. // Set/Get current position
  309. template <class Ktype, class Vtype>
  310. inline CoolAssociation_state& CoolAssociation<Ktype,Vtype>::current_position () {
  311.   return CoolVector<CoolPair<Ktype,Vtype>>::current_position();
  312. }
  313.  
  314.  
  315. #endif                // End #ifdef of ASSOCIATIONH
  316.